package com.wiz.weixin.controllor;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;

import org.json.JSONException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.wiz.weixin.TO.PrPriceTO;
import com.wiz.weixin.TO.WeixinConfigTO;
import com.wiz.weixin.manager.TencentManager;
import com.wiz.weixin.service.PrPriceService;
import com.wiz.weixin.service.StationService;
import com.wiz.weixin.service.WeixinConfigService;

/**
 * 优惠
 * @author xxy
 *
 */
@Controller
@RequestMapping("/discount")
public class DiscountControllor {
	private transient final Logger log=LoggerFactory.getLogger(getClass());
	@Resource
	private StationService stationService;
	@Resource
	private WeixinConfigService weixinConfigService;
	@Resource
	private TencentManager tencentManager;
	
	/**充值优惠*/
	@RequestMapping(value="/listSaleInfo")
	public String listSaleInfo(HttpServletRequest request,HttpServletResponse response,
			HttpSession session, Model model) {
		String code = request.getParameter("code");
		try {
			String openid = (String) session.getAttribute("openid");
			if(openid == null || openid.equals("")){
				openid =tencentManager.getOpenId(code);
				session.setAttribute("openid", openid);
			}
			//查询客户的所有会员
			List<String> hqIds =  (List<String>) session.getAttribute("hqIds");
			if(hqIds == null || hqIds.size()<= 0){
				hqIds =  stationService.getHQIds(openid);
				session.setAttribute("hqIds", hqIds);
			}
			Map<String,List<WeixinConfigTO>> map = weixinConfigService.listSaleInfo(hqIds);
			model.addAttribute("map", map);
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
		}
		return "/discount/listSaleInfo";
		
	}
	/**积分活动*/
	@RequestMapping(value="/activity")
	public String listPointInfo(HttpServletRequest request,HttpServletResponse response,
			HttpSession session, Model model) {
		String code = request.getParameter("code");
		try {
			String openid = (String) session.getAttribute("openid");
			if(openid == null || openid.equals("")){
				openid = tencentManager.getOpenId(code);
				session.setAttribute("openid", openid);
			}
			//查询客户的所有会员
			List<String> hqIds =  (List<String>) session.getAttribute("hqIds");
    		if(hqIds == null || hqIds.size()<= 0){
    			hqIds = stationService.getHQIds(openid);
    			session.setAttribute("hqIds", hqIds);
    		}
    		Map<String,List<WeixinConfigTO>> map = weixinConfigService.listPointInfo(hqIds);
    		model.addAttribute("map", map);
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
		}
		return "/discount/activity";
		
	}
}
